/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormDBarang.java
 *
 * Created on Feb 2, 2011, 4:47:59 PM
 */

package juwita;

import config.DBConnection;
import java.awt.CardLayout;
import java.sql.*;
//import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
import javax.swing.table.TableColumn;

/**
 *
 * @author Abah
 */
public class FormDBarang extends javax.swing.JInternalFrame {

    private DefaultTableModel DftTabModeDBrg;
    private DefaultTableModel DftTabModeBrg;
    java.sql.Connection conn = new DBConnection().connect();
    /** Creates new form FormDBarang */
    public FormDBarang() {
        initComponents();
        btnDEdit.setEnabled(false);        
        tbDBrg.getTableHeader().setReorderingAllowed(false);
        tampilDataDBrg();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnTengah = new javax.swing.JPanel();
        pnAtas = new javax.swing.JPanel();
        txtKdBrg = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtKet = new javax.swing.JTextArea();
        txtNmBrg = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtDPrs = new javax.swing.JTextField();
        ckDBerlaku = new javax.swing.JCheckBox();
        txtDRp = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        btnDEdit = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        pnShow = new javax.swing.JPanel();
        pnDBrg = new javax.swing.JPanel();
        spDBrg = new javax.swing.JScrollPane();
        tbDBrg = new javax.swing.JTable();
        pnSearchDiscBrg = new javax.swing.JPanel();
        btnDSearch = new javax.swing.JButton();
        btnDShowAll = new javax.swing.JButton();
        txtSrcDiscBrg = new javax.swing.JTextField();
        cbSrcDisc = new javax.swing.JComboBox();
        infoDBrg = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Pengaturan Discount Barang");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/disc_brg.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        pnTengah.setBackground(new java.awt.Color(255, 255, 255));
        pnTengah.setLayout(new java.awt.BorderLayout());

        pnAtas.setBackground(new java.awt.Color(255, 255, 255));
        pnAtas.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 255), 2));
        pnAtas.setPreferredSize(new java.awt.Dimension(410, 200));
        pnAtas.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtKdBrg.setEditable(false);
        txtKdBrg.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        pnAtas.add(txtKdBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 10, 110, -1));

        jLabel2.setText("Kode Barang");
        pnAtas.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        jLabel4.setText("Keterangan");
        pnAtas.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 120, -1, -1));

        txtKet.setColumns(20);
        txtKet.setRows(5);
        jScrollPane1.setViewportView(txtKet);

        pnAtas.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 120, 240, 60));

        txtNmBrg.setEditable(false);
        pnAtas.add(txtNmBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 40, 240, -1));

        jLabel5.setText("Nama Barang");
        pnAtas.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));

        jLabel6.setText("Discount %");
        pnAtas.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, -1, -1));

        txtDPrs.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtDPrs.setText("0");
        txtDPrs.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtDPrsFocusLost(evt);
            }
        });
        txtDPrs.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDPrsKeyPressed(evt);
            }
        });
        pnAtas.add(txtDPrs, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 90, 40, -1));

        ckDBerlaku.setBackground(new java.awt.Color(255, 255, 255));
        ckDBerlaku.setText("Berlaku");
        pnAtas.add(ckDBerlaku, new org.netbeans.lib.awtextra.AbsoluteConstraints(280, 10, -1, -1));

        txtDRp.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtDRp.setText("0");
        txtDRp.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtDRpFocusLost(evt);
            }
        });
        txtDRp.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtDRpKeyPressed(evt);
            }
        });
        pnAtas.add(txtDRp, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 90, 100, -1));

        jLabel7.setText("Discout Rp");
        pnAtas.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 90, -1, -1));

        btnDEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/btn_edit.png"))); // NOI18N
        btnDEdit.setText("Edit");
        btnDEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDEditActionPerformed(evt);
            }
        });
        pnAtas.add(btnDEdit, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 110, 90, 40));

        jLabel8.setForeground(new java.awt.Color(0, 0, 204));
        jLabel8.setText("*pilih salah satu");
        pnAtas.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 70, -1, -1));

        pnTengah.add(pnAtas, java.awt.BorderLayout.PAGE_START);

        pnShow.setLayout(new java.awt.CardLayout());

        pnDBrg.setLayout(new java.awt.BorderLayout());

        spDBrg.setAutoscrolls(true);

        tbDBrg.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbDBrg.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tbDBrg.setGridColor(new java.awt.Color(0, 0, 255));
        tbDBrg.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbDBrgMousePressed(evt);
            }
        });
        tbDBrg.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbDBrgKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbDBrgKeyReleased(evt);
            }
        });
        spDBrg.setViewportView(tbDBrg);

        pnDBrg.add(spDBrg, java.awt.BorderLayout.CENTER);

        pnSearchDiscBrg.setBackground(new java.awt.Color(255, 255, 255));
        pnSearchDiscBrg.setPreferredSize(new java.awt.Dimension(0, 70));
        pnSearchDiscBrg.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnDSearch.setText("Search");
        btnDSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDSearchActionPerformed(evt);
            }
        });
        pnSearchDiscBrg.add(btnDSearch, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 40, 80, -1));

        btnDShowAll.setText("Show All");
        btnDShowAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDShowAllActionPerformed(evt);
            }
        });
        pnSearchDiscBrg.add(btnDShowAll, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 10, 80, -1));
        pnSearchDiscBrg.add(txtSrcDiscBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 40, 220, 20));

        cbSrcDisc.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kode Disc", "Nama Brg" }));
        pnSearchDiscBrg.add(cbSrcDisc, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 90, 20));

        infoDBrg.setFont(new java.awt.Font("Tahoma", 1, 14));
        infoDBrg.setForeground(new java.awt.Color(0, 0, 204));
        infoDBrg.setText("Search Data Diskon Barang");
        pnSearchDiscBrg.add(infoDBrg, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, 20));

        pnDBrg.add(pnSearchDiscBrg, java.awt.BorderLayout.PAGE_START);

        pnShow.add(pnDBrg, "dbarang");

        pnTengah.add(pnShow, java.awt.BorderLayout.CENTER);

        getContentPane().add(pnTengah, java.awt.BorderLayout.CENTER);

        setBounds(0, 0, 508, 454);
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
        int clossing;
        clossing = JOptionPane.showConfirmDialog(this,
                "Tutup Pengaturan Discount Barang?",
                "Konfirmasi",
                JOptionPane.YES_NO_OPTION);
        if (clossing == 0){
            this.dispose();
            FormMain.isfDBrg = false;
        }
    }//GEN-LAST:event_formInternalFrameClosing

    private void tbDBrgMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDBrgMousePressed
        getDataFromDBarang();
}//GEN-LAST:event_tbDBrgMousePressed

    private void tbDBrgKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbDBrgKeyPressed
        // TODO add your handling code here:
        getDataFromDBarang();
    }//GEN-LAST:event_tbDBrgKeyPressed

    private void tbDBrgKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbDBrgKeyReleased
        // TODO add your handling code here:
        getDataFromDBarang();
}//GEN-LAST:event_tbDBrgKeyReleased

    private void btnDShowAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDShowAllActionPerformed
        // TODO add your handling code here:
        tampilDataDBrg();
}//GEN-LAST:event_btnDShowAllActionPerformed

    private void btnDSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDSearchActionPerformed
        // TODO add your handling code here:
        tampilSearchDiscBrg();
        //txtSrcDiscBrg.setText("");
        //txtSrcDiscBrg.requestFocus();
        btnDEdit.setEnabled(false);
}//GEN-LAST:event_btnDSearchActionPerformed

    private void btnDEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDEditActionPerformed
        // TODO add your handling code here:        
        editData();        
    }//GEN-LAST:event_btnDEditActionPerformed

    private void txtDPrsKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDPrsKeyPressed
        // TODO add your handling code here:
        if (!txtDPrs.getText().equals("0")){
            txtDRp.setText("0");
        }
    }//GEN-LAST:event_txtDPrsKeyPressed

    private void txtDRpKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDRpKeyPressed
        // TODO add your handling code here:
        if (!txtDRp.getText().equals("0")){
            txtDPrs.setText("0");
        }
    }//GEN-LAST:event_txtDRpKeyPressed

    private void txtDPrsFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDPrsFocusLost
        // TODO add your handling code here:
        if (!txtDPrs.getText().equals("0")){
            txtDRp.setText("0");
        }
    }//GEN-LAST:event_txtDPrsFocusLost

    private void txtDRpFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtDRpFocusLost
        // TODO add your handling code here:
        if (!txtDRp.getText().equals("0")){
            txtDPrs.setText("0");
        }
    }//GEN-LAST:event_txtDRpFocusLost


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDEdit;
    private javax.swing.JButton btnDSearch;
    private javax.swing.JButton btnDShowAll;
    private javax.swing.JComboBox cbSrcDisc;
    private javax.swing.JCheckBox ckDBerlaku;
    private javax.swing.JLabel infoDBrg;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnAtas;
    private javax.swing.JPanel pnDBrg;
    private javax.swing.JPanel pnSearchDiscBrg;
    private javax.swing.JPanel pnShow;
    private javax.swing.JPanel pnTengah;
    private javax.swing.JScrollPane spDBrg;
    private javax.swing.JTable tbDBrg;
    private javax.swing.JTextField txtDPrs;
    private javax.swing.JTextField txtDRp;
    private javax.swing.JTextField txtKdBrg;
    private javax.swing.JTextArea txtKet;
    private javax.swing.JTextField txtNmBrg;
    private javax.swing.JTextField txtSrcDiscBrg;
    // End of variables declaration//GEN-END:variables

    private void kosongkan(){    
        txtKdBrg.setText("");
        txtNmBrg.setText("");
        txtDPrs.setText("");
        txtDRp.setText("");
        txtKet.setText("");
        txtSrcDiscBrg.setText("");
        btnDEdit.setEnabled(false);        
        btnDEdit.setText("Edit");        
        ckDBerlaku.setSelected(false);
    }
    
    private void tampilDataDBrg(){
        Object[] Baris={"Kode", "Nama Brg", "Disc %", "Disc Rp"};
        DftTabModeDBrg=new DefaultTableModel(null,Baris);
        tbDBrg.setModel(DftTabModeDBrg);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("SELECT * from tbrg");
            while (rslt.next()) {                
                String sKdBrg = rslt.getString("kdBrg");                
                String sNmBrg = rslt.getString("nmBrg");
                String sPrs = rslt.getString("disc_prs");
                String sRp = rslt.getString("disc_rp");

                String[] dataField = {sKdBrg, sNmBrg, sPrs, sRp};
                DftTabModeDBrg.addRow(dataField);
                int widths[] = {60, 270, 50, 100};
                for(int i=0;i<tbDBrg.getColumnCount();i++){
                    TableColumn column = tbDBrg.getColumnModel().getColumn(i);
                    column.setPreferredWidth(widths[i]);
                }
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }

    private void tampilSearchDiscBrg(){
        String srcby = "";
        if(cbSrcDisc.getSelectedItem().toString().equals("Kode Disc")){
            srcby = "kdBrg";
        }else if(cbSrcDisc.getSelectedItem().toString().equals("Nama Brg")){
            srcby = "nmBrg";
        }

        Object[] Baris={"Kode","Nama Brg", "Disc %", "Disc Rp"};
        DftTabModeDBrg=new DefaultTableModel(null,Baris);
        tbDBrg.setModel(DftTabModeDBrg);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT * FROM tbrg " +
                    "WHERE UPPER(" + srcby +
                    ") like UPPER('%" +
                    txtSrcDiscBrg.getText() + "%')");
            while (rslt.next()) {                
                String sKdBrg = rslt.getString("kdBrg");
                String sNmBrg = rslt.getString("nmBrg");
                String sDiscPrs = rslt.getString("disc_prs");
                String sDiscRp = rslt.getString("disc_rp");
                //String sNamaKota = rslt.getString("ket");

                String[] dataField = {sKdBrg, sNmBrg, sDiscPrs, sDiscRp};
                DftTabModeDBrg.addRow(dataField);
                int widths[] = {60, 270, 50, 100};
                for(int i=0;i<tbDBrg.getColumnCount();i++){
                    TableColumn column = tbDBrg.getColumnModel().getColumn(i);
                    column.setPreferredWidth(widths[i]);
                }
            }

        }
        catch(SQLException e) {
            System.out.println("gagal! : " + e.toString());
        }
    }

    private void editData(){
         
        boolean selected = ckDBerlaku.isSelected();
        String berlaku = "";
        if (selected){
            berlaku ="1";
        }else{
            berlaku = "0";
        }
        try {
            PreparedStatement pStatement = null;
            String sql = "UPDATE tbrg " +
                    "SET ket=?, " +
                    "disc_prs=?, disc_rp=?, " +
                    "berlaku=? " +
                    "WHERE kdBrg=? ";
            pStatement = conn.prepareStatement(sql);

            //pStatement.setString(1, cbKodeKota.getSelectedItem().toString());
            
            pStatement.setString(1, txtKet.getText());
            pStatement.setFloat(2, Float.parseFloat(txtDPrs.getText()));
            pStatement.setDouble(3, Double.parseDouble(txtDRp.getText()));
            pStatement.setString(4, berlaku);
            pStatement.setString(5, txtKdBrg.getText());

            int intTambah = pStatement.executeUpdate();
            if (intTambah >0){
                JOptionPane.showMessageDialog(this,
                        "Edit sukses", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);

            }else {
                JOptionPane.showMessageDialog(this,
                        "Edit gagal", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }

            
            pStatement.close();
            kosongkan();
            tampilDataDBrg();


        } catch(SQLException e){
            System.out.println("koneksi gagal " + e.toString());
        }
    }
    private void getDataFromDBarang(){
                // TODO add your handling code here:        
        Object oKdBrg, oNmBrg, oDiscPrs, oDiscRp;//, oTest;

        oKdBrg = tbDBrg.getValueAt(tbDBrg.getSelectedRow(), 0);
        oNmBrg = tbDBrg.getValueAt(tbDBrg.getSelectedRow(), 1);
        oDiscPrs = tbDBrg.getValueAt(tbDBrg.getSelectedRow(), 2);
        oDiscRp = tbDBrg.getValueAt(tbDBrg.getSelectedRow(), 3);        

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("SELECT * FROM tbrg " +
                    "WHERE kdBrg=" +
                    "'" +oKdBrg+ "'");
            while (rslt.next()) {                
                txtKet.setText(rslt.getString("ket"));
                if(rslt.getString("berlaku").toString().equals("1")){
                    ckDBerlaku.setSelected(true);
                }else{
                    ckDBerlaku.setSelected(false);
                }
            }

        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
        txtKdBrg.setText(oKdBrg.toString());
        txtNmBrg.setText(oNmBrg.toString());
        txtDPrs.setText(oDiscPrs.toString());
        txtDRp.setText(oDiscRp.toString());

        btnDEdit.setEnabled(true);
    }
        
}
